package com.cc.main.cmd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cc.main.Const;
import com.cc.main.ICommonCmd;
import com.cc.main.problems.bean.KeyWordItem;
import com.cc.main.problems.bean.ProblemBean;
import com.cc.main.util.MyUtils;

/**
 * 添加关键字指令
 * @author TR
 *
 */
public class AddKeywordCmd implements ICommonCmd{
	
	private List<KeyWordItem> appendKwList = new ArrayList<KeyWordItem>();

	@Override
	public void execute(Object action) {
		// TODO Auto-generated method stub
		System.out.println("请输入筛选的关键字信息。(ls:打印新增关键字列表 rm:删除关键字 clear:清空关键字 quit：退出)");
		List<KeyWordItem> nkw = new ArrayList<>();
		while(true) {
			String input = MyUtils.getInput(Const.MAddKeywordCmd);
			if(input.length() == 0) {
				continue;
			}
			if(input.equalsIgnoreCase("quit")) {
//				appendKwList.addAll(nkw);
				appendList(nkw);
				nkw.clear();
				break;
			}
			
			if(input.equalsIgnoreCase("clear")) {
				appendKwList.clear();
				nkw.clear();
				continue;
			}
			
			if(input.equalsIgnoreCase("ls")) {
				for(KeyWordItem item:appendKwList) {
					System.out.println(item.key);
				}
				for(KeyWordItem item:nkw) {
					System.out.println(item.key);
				}
				continue;
			}
			if(input.startsWith("rm")) {
				if(input.length() == 2) {
					continue;
				}
				String key = input.substring(3).trim();
				if(key.isEmpty()) {
					continue;
				}
				Iterator<KeyWordItem> iter = nkw.iterator();
				while(iter.hasNext()) {
					String item = iter.next().key;
					if(item.contains(key)) {
						iter.remove();
					}
				}
				
				iter = appendKwList.iterator();
				while(iter.hasNext()) {
					String item = iter.next().key;
					if(item.contains(key)) {
						iter.remove();
					}
				}
				continue;
			}
			
			if(!isExist(nkw, input)) {
				nkw.add(new KeyWordItem(input));
			}
		}
		ProblemBean.getInstance().addkeyList.clear();
		ProblemBean.getInstance().addkeyList.addAll(appendKwList);
	}
	
	@Override
	public String printCurrentEnv() {
		// TODO Auto-generated method stub
		String cont = Const.MAddKeywordCmd + ":";
		for(KeyWordItem item:appendKwList) {
			cont += "{" + item.key + "} ";
		}
		return cont;
	}
	
	/**
	 * 添加inList 到appendKwList
	 * @param inList
	 */
	private void appendList(List<KeyWordItem> inList) {
		if(appendKwList.isEmpty()) {
			appendKwList.addAll(inList);
		}else {
			List<KeyWordItem> notExistList = new ArrayList<>();
			for(KeyWordItem initem:inList) {
				boolean isFinded = false;
				for(KeyWordItem item:appendKwList) {
					if(initem.key.equalsIgnoreCase(item.key)) {
						isFinded  = true;
					}
				}
				if(!isFinded) {
					notExistList.add(initem);
				}
			}
			appendKwList.addAll(notExistList);
		}
	}
	
	/**
	 * cont 是否在list中
	 * @param list
	 * @param cont
	 * @return
	 */
	private boolean isExist(List<KeyWordItem> list,String cont) {
		for(KeyWordItem item:list) {
			if(item.key.equalsIgnoreCase(cont)) {
				return true;
			}
		}
		for(KeyWordItem item:appendKwList) {
			if(item.key.equalsIgnoreCase(cont)) {
				return true;
			}
		}
		return false;
	}
}
